﻿using System;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Devices;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace SnapChatReplacement
{
    public partial class newSnap : PhoneApplicationPage
    {
        public newSnap()
        {
            InitializeComponent();
            this.ApplicationBar = new ApplicationBar();
            this.ApplicationBar.Mode = ApplicationBarMode.Default;
            this.ApplicationBar.Opacity = 1;
            this.ApplicationBar.IsVisible = true;
            this.ApplicationBar.IsMenuEnabled = false;

            ApplicationBarIconButton button1 = new ApplicationBarIconButton();
            button1.IconUri = new Uri("/Assets/feature.camera.png", UriKind.Relative);
            button1.Text = "Take image";
            button1.Click += captureImage;
            this.ApplicationBar.Buttons.Insert(0, button1);
            

            ApplicationBarIconButton button2 = new ApplicationBarIconButton();
            button2.IconUri = new Uri("/Assets/sync.png", UriKind.Relative);
            button2.Text = "Change camera";
            button2.Click += changeCamera;
            this.ApplicationBar.Buttons.Insert(1, button2);
            
        }

        private void captureImage(object sender, EventArgs e)
        {
            if (cam != null)
            {
                cam.CaptureImage();
            }
        }

        private void initCamera(bool isFront)
        {
            if(cam != null)
                cam.Dispose();
            if (isFront)
            {
                cam = new PhotoCamera(CameraType.FrontFacing);
            }
            else
            {
                cam = new PhotoCamera(CameraType.Primary);
            }

            viewfinderBrush.SetSource(cam);
            cam.CaptureImageAvailable += cam_CaptureImageAvailable;
        }

        private async void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
        {
            byte[] buffer = new byte[e.ImageStream.Length];
            await e.ImageStream.ReadAsync(buffer,0, (int)e.ImageStream.Length);
        }

        private void changeCamera(object sender, EventArgs e)
        {
            cam.Dispose();
            if (cam.CameraType == CameraType.Primary)
            {
                   initCamera(true);
                   viewfinderTransform.Rotation = -90;
            }
             
            else
            {
                initCamera(false);
                viewfinderTransform.Rotation = 90;
            }
            

        }

        private PhotoCamera cam;
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true)
            {
                initCamera(false);
            }
        }

        private void viewfinder_Tapped(object sender, GestureEventArgs e)
        {
        
        }

        protected override void OnOrientationChanged(OrientationChangedEventArgs e)
        {
            if (cam != null && cam.CameraType == CameraType.Primary)
            {
                Dispatcher.BeginInvoke(() =>
                    {
                        double rotation = cam.Orientation;
                        switch (this.Orientation)
                        {
                            case PageOrientation.LandscapeLeft:
                                rotation = cam.Orientation - 90;
                                break;
                            case PageOrientation.LandscapeRight:
                                rotation = cam.Orientation + 90;
                                break;
                        }
                        viewfinderTransform.Rotation = rotation;
                    });
            }
            base.OnOrientationChanged(e);
            
        }
    }

}